
clear all;
close all;
clc

cd E:\ReplicateBuild\02_code\03_matlab

store_suffix='imputed';

%%%
% restricted timing
%%%
base=['alloc_teach_est_princ_est_restr_',store_suffix,'.mat'];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);


basedata=open(savefile);

balanced_restr=basedata.balanced;
school_short_restr=basedata.school_short;

%%%
% restricted timing, noisy
%%%
base=['alloc_teach_est_princ_noisy_restr_',store_suffix,'.mat'];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);


basedata=open(savefile);

balanced_noisy=basedata.balanced;
school_short_noisy=basedata.school_short;


%%%
% unrestricted timing
%%%
base=['alloc_teach_est_princ_est_unrestr_',store_suffix,'.mat'];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);


basedata=open(savefile);

balanced_unrestr=basedata.balanced;
school_short_unrestr=basedata.school_short;

%%%
% restricted timing, principal max va 
%%%
base=['alloc_teach_est_princ_va_restr_',store_suffix,'.mat'];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);


basedata=open(savefile);

balanced_va=basedata.balanced;
school_short_va=basedata.school_short;


%%%
% restricted timing, teach max ndis
%%%
base=['alloc_teach_ndis_princ_est_restr_',store_suffix,'.mat'];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);


basedata=open(savefile);

balanced_ndis=basedata.balanced;
school_short_ndis=basedata.school_short;



%%%
% restricted timing, teach max ndis, principal max va
%%%
base=['alloc_teach_ndis_princ_va_restr_',store_suffix,'.mat'];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);


basedata=open(savefile);

balanced_both=basedata.balanced;
school_short_both=basedata.school_short;


%%%  
%  6. status quo, principal max VA, unrestricted choice sets, also compute first best 
%%%

base=['alloc_teach_est_princ_va_unrestr_',store_suffix,'.mat'];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);


basedata=open(savefile);

balanced_fb=basedata.balanced;
school_short_fb=basedata.school_short;



%%
% MATLAB script to produce a LaTeX table with dynamic data

% Specify the directory path where you want to save the .tex file
save_path = 'E:\ReplicateBuild\03_output\tables\';

% Complete file path with file name
file_name = [save_path 'imputed_teachers.tex'];

% Open a file to write
fileID = fopen(file_name,'w');

% Check if the file was opened successfully
if fileID == -1
    error('File could not be opened, check if the path is correct and writable.');
end

%[va_disadv va_adv fracdisadv_matched fracdisadv_unmatched va_disadv_unmatch];
% Write the table header and begin the tabular environment
fprintf(fileID, ' \\begin{tabular}{lcccccc} \\hline \\noalign{\\smallskip} \\multicolumn{1}{l}{Description} & \\multicolumn{1}{l}{VA disadv.} & \\multicolumn{1}{l}{VA adv.} & \\multicolumn{4}{c}{Teacher-weighted VA by student type} \\noalign{\\smallskip}  \\\\\\hline\n');
fprintf(fileID, '&&& \\multicolumn{2}{c}{Imputed} & \\multicolumn{2}{c}{Nonimputed}  \\\\\n');
fprintf(fileID, '&&& Disadv. & Adv. & Disadv. & Adv.  \\\\\\hline\n');
fprintf(fileID, '\\noalign{\\smallskip} && \\multicolumn{4}{l}{\\textbf{A. Balanced market   }} \\noalign{\\smallskip}  \\\\\\hline\n');
fprintf(fileID, ' \\noalign{\\smallskip} Status quo & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', balanced_restr);
%fprintf(fileID, 'Noisy hiring & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\\hline\n', balanced_noisy);
fprintf(fileID, 'All options (timing) & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\n', balanced_unrestr);
fprintf(fileID, 'Teachers N Dis & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\n', balanced_ndis);
fprintf(fileID, 'Principals VA & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\n', balanced_va);
fprintf(fileID, 'Previous two & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', balanced_both);
fprintf(fileID, 'First best & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\\hline \n', balanced_fb);
fprintf(fileID, '\\noalign{\\smallskip} && \\multicolumn{4}{l}{\\textbf{B. Unbalanced market: schools short   }} \\noalign{\\smallskip} \\\\\\hline\n');
fprintf(fileID, ' \\noalign{\\smallskip} Status quo & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\n', school_short_restr);
%fprintf(fileID, 'Noisy hiring & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\\hline\n', school_short_noisy);
fprintf(fileID, 'All options (timing) & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', school_short_unrestr);
fprintf(fileID, 'Teachers N Dis & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\n', school_short_ndis);
fprintf(fileID, 'Principals VA & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\n', school_short_va);
fprintf(fileID, 'Previous two & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\n', school_short_both);
fprintf(fileID, 'First best & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\n', school_short_fb);
% End the tabular environment and close the table
fprintf(fileID, '\\noalign{\\smallskip}  \\hline  \\end{tabular}\n');

% Close the file
fclose(fileID);

% Notify the user
fprintf('LaTeX table saved to %s\n', file_name);
